package cn.source.web.controller.zk;

import cn.source.common.core.controller.BaseController;
import cn.source.common.core.domain.AjaxResult;
import cn.source.common.core.domain.model.LoginUser;
import cn.source.common.core.page.TableDataInfo;
import cn.source.common.utils.poi.ExcelUtil;
import cn.source.zk.domain.dto.SettingQueryFieldsDTO;
import cn.source.zk.domain.dto.TableDefinitionDTO;
import cn.source.zk.domain.entity.ZkSettingQueryFields;
import cn.source.zk.domain.entity.ZkTableDefinition;
import cn.source.zk.domain.vo.TableDefinitionVO;
import cn.source.zk.domain.vo.ZkSettingQueryFieldsVO;
import cn.source.zk.service.ZkSettingQueryFieldsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author shiqi
 * @create 2025-04-18 14:14
 */
@RestController
@RequestMapping("/zk/setting/query/fields")
@Slf4j
public class ZkSettingQueryFieldsController extends BaseController {

    @Autowired
    private ZkSettingQueryFieldsService zkSettingQueryFieldsService;


    /**
     * 查询字段分页列表
     * @param dto
     * @return
     */
    @GetMapping("list")
    public TableDataInfo listPage(SettingQueryFieldsDTO dto) {
        startPage();
        List<ZkSettingQueryFieldsVO> list  = zkSettingQueryFieldsService.listPage(dto);
        return getDataTable(list);
    }


    /**
     * 新增字段查询
     * @param zkSettingQueryFields
     * @return
     */
    @PostMapping("addSettingQueryFields")
    public AjaxResult addSettingQueryFields(@RequestBody ZkSettingQueryFields zkSettingQueryFields) {
        log.info("zkSettingQueryFields: {}", zkSettingQueryFields);
        zkSettingQueryFields.setCreator(getUsername());
        zkSettingQueryFields.setModifier(getUsername());
        zkSettingQueryFields.setCreateTime(LocalDateTime.now());
        zkSettingQueryFields.setUpdateTime(LocalDateTime.now());
        zkSettingQueryFieldsService.addSettingQueryFields(zkSettingQueryFields);
        return AjaxResult.success();
    }

    /**
     * 修改字段查询
     * @param zkSettingQueryFields
     * @return
     */
    @PostMapping("editSettingQueryFields")
    public AjaxResult editSettingQueryFields(@RequestBody ZkSettingQueryFields zkSettingQueryFields) {
        zkSettingQueryFields.setModifier(getUsername());
        zkSettingQueryFields.setUpdateTime(LocalDateTime.now());
        zkSettingQueryFieldsService.editSettingQueryFields(zkSettingQueryFields);
        return AjaxResult.success();
    }

    /**
     * 根据id查询字段查询
     * @param id
     * @return
     */
    @GetMapping("getSettingQueryFieldsById/{id}")
    public AjaxResult getSettingQueryFieldsById(@PathVariable Integer id) {
        ZkSettingQueryFields zkSettingQueryFields = zkSettingQueryFieldsService.getSettingQueryFieldsById(id);
        return AjaxResult.success(zkSettingQueryFields);
    }

    /**
     * 删除字段查询
     * @param id
     * @return
     */
    @DeleteMapping("deleteSettingQueryFields/{id}")
    public AjaxResult npm(@PathVariable Integer id) {
        zkSettingQueryFieldsService.deleteSettingQueryFields(id);
        return AjaxResult.success();
    }

    /**
     * 导入方法
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<ZkSettingQueryFields> util = new ExcelUtil<ZkSettingQueryFields>(ZkSettingQueryFields.class);
        List<ZkSettingQueryFields> userList = util.importExcel(file.getInputStream());
        LoginUser loginUser = getLoginUser();
        String operName = loginUser.getUsername();
        String message = zkSettingQueryFieldsService.importUser(userList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    /**
     * 下载导入模板
     * @return
     */
    @PostMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<ZkSettingQueryFields> util = new ExcelUtil<ZkSettingQueryFields>(ZkSettingQueryFields.class);
        return util.importTemplateExcel("表格数据数据");
    }



    /**
     * 查询表标识分页列表
     * @param dto
     * @return
     */
    @GetMapping("tableDefinition/list")
    public TableDataInfo tableDefinitionListPage(TableDefinitionDTO dto) {
        startPage();
        List<TableDefinitionVO> list  = zkSettingQueryFieldsService.tableDefinitionListPage(dto);
        return getDataTable(list);
    }

    /**
     * 新增表标识
     * @param tableDefinition
     * @return
     */
    @PostMapping("addTableDefinition")
    public AjaxResult addTableDefinition(@RequestBody ZkTableDefinition tableDefinition) {

        tableDefinition.setCreator(getUsername());
        tableDefinition.setModifier(getUsername());
        tableDefinition.setCreateTime(LocalDateTime.now());
        tableDefinition.setUpdateTime(LocalDateTime.now());
        zkSettingQueryFieldsService.addTableDefinition(tableDefinition);
        return AjaxResult.success();
    }

    /**
     * 修改表标识
     * @param tableDefinition
     * @return
     */
    @PostMapping("editTableDefinition")
    public AjaxResult editTableDefinition(@RequestBody ZkTableDefinition tableDefinition) {
        tableDefinition.setModifier(getUsername());
        tableDefinition.setUpdateTime(LocalDateTime.now());
        zkSettingQueryFieldsService.editTableDefinition(tableDefinition);
        return AjaxResult.success();
    }

    @GetMapping("getTableDefinitionById/{id}")
    public AjaxResult getTableDefinitionById(@PathVariable Integer id) {
        ZkTableDefinition zkTableDefinition = zkSettingQueryFieldsService.getTableDefinitionById(id);
        return AjaxResult.success(zkTableDefinition);
    }
    /**
     * 删除表标识
     * @param id
     * @return
     */
    @DeleteMapping("/deleteTableDefinition/{id}")
    public AjaxResult deleteTableDefinition(@PathVariable Integer id) {
        zkSettingQueryFieldsService.deleteTableDefinition(id);
        return AjaxResult.success();
    }

    /**
     * 获取多个表的表头
     * @return
     */
    @GetMapping("/getIhrData")
    public AjaxResult getIhrData(SettingQueryFieldsDTO dto){
        List<ZkSettingQueryFieldsVO> list  = zkSettingQueryFieldsService.getIhrData(dto);
        return AjaxResult.success(list);
    }


}
